package com.iking.toolkit.formatdeal.service.impl;

import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iking.toolkit.formatdeal.async.FileFormatAsync;
import com.iking.toolkit.formatdeal.enums.FileFormatPathEnum;
import com.iking.toolkit.formatdeal.model.FileFormatConversion;
import com.iking.toolkit.formatdeal.dao.FileFormatConversionMapper;
import com.iking.toolkit.formatdeal.model.dto.SelectByPageDTO;
import com.iking.toolkit.formatdeal.service.FileFormatConversionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iking.toolkit.model.EmergencyUtil;
import com.iking.toolkit.model.Msg;
import com.iking.toolkit.model.PageRes;
import com.iking.toolkit.util.FileFormatUtil;
import com.iking.toolkit.util.SysUtil;
import com.iking.toolkit.util.minio.MinioStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件格式转换任务 ServiceImpl
 * </p>
 *
 * @author lqb
 * @since 2025-08-04
 */
@Slf4j
@Service
public class FileFormatConversionServiceImpl extends ServiceImpl<FileFormatConversionMapper, FileFormatConversion> implements FileFormatConversionService {

    @Autowired
    private MinioStorage minioStorage;

    @Autowired
    private FileFormatAsync fileFormatAsync;

    /**
     * 功能描述: 执行文件格式转换
     *
     * @param: [multipartFile, uploadId, fileFormatPathEnum]
     * @author: yfl
     * @date: 2025/8/6 12:00
     */
    @Override
    public Msg fileFormatConversion(MultipartFile multipartFile, String uploadId, FileFormatPathEnum fileFormatPathEnum) {
        // 获取文件名和扩展名
        String fileName = multipartFile.getOriginalFilename();
        String fileSuffix = FileFormatUtil.getFileSuffix(fileName);
        String beforeFilePath = fileFormatPathEnum.getPath() + "/before/" + SysUtil.getUUID() + "." + FileUtil.extName(fileName);
        // 上传转换前文件
        minioStorage.uploadFile(multipartFile, beforeFilePath, uploadId);
        // 获取文件大小并转换为合适单位
        long fileSizeBytes = multipartFile.getSize();
        String fileSizeInfo = FileFormatUtil.formatFileSize(fileSizeBytes);
        // 转换任务入库
        FileFormatConversion fileFormatConversion = new FileFormatConversion();
        fileFormatConversion.setFileFormatConversionId(SysUtil.getUUID());
        fileFormatConversion.setBusinessId(uploadId);
        fileFormatConversion.setBeforeFileName(fileName);
        fileFormatConversion.setBeforeFileFormat(fileSuffix);
        fileFormatConversion.setBeforeFileSize(fileSizeInfo);
        fileFormatConversion.setBeforeFileUrl(beforeFilePath);
        fileFormatConversion.setConversionType(fileFormatPathEnum.getType());
        fileFormatConversion.setConversionAction(fileFormatPathEnum.getAction());
        fileFormatConversion.setConversionState(1);
        fileFormatConversion.setStartTime(new Date());
        baseMapper.insert(fileFormatConversion);
        // 2. 将MultipartFile转为byte[]（核心：脱离请求上下文）
        byte[] fileBytes = null;
        try {
            fileBytes = multipartFile.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 异步执行格式转换
        fileFormatAsync.fileFormatThreadPoolAsync(fileBytes, multipartFile.getOriginalFilename(), multipartFile.getContentType(), fileFormatConversion, fileFormatPathEnum);
        return Msg.sucess();
    }

    /**
     * 功能描述: 根据业务id查询文件格式转换记录
     *
     * @param: [businessId]
     * @author: yfl
     * @date: 2025/8/7 10:06
     */
    @Override
    public Msg<FileFormatConversion> selectByBusinessId(String businessId) {
        FileFormatConversion fileFormatConversion = baseMapper.selectByBusinessId(businessId);
        return Msg.sucess(fileFormatConversion);
    }

    /**
     * 功能描述: 分页查询文件格式转换记录
     *
     * @param: [selectByPageDTO]
     * @author: yfl
     * @date: 2025/8/7 10:17
     */
    @Override
    public Msg<PageRes<FileFormatConversion>> selectByPage(SelectByPageDTO selectByPageDTO) {
        PageHelper.startPage(selectByPageDTO.getCurrentPage(), selectByPageDTO.getPageSize());
        Page<FileFormatConversion> list = baseMapper.selectByPage(selectByPageDTO);
        PageRes<FileFormatConversion> pageRes = EmergencyUtil.conversionPage(list);
        return Msg.sucess(pageRes);
    }
}
